package se.plingapp.enterprisebeans;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import se.plingapp.entitybeans.Comment;
import se.plingapp.entitybeans.Content;

/**
 * Comment business.
 *
 * @author Jens Ljungblad
 */
@Stateless
public class CommentFacade extends AbstractFacade<Comment> {

    /**
     * Entity manager with a given persistence context.
     */
    @PersistenceContext(unitName = "PlingPU")
    private EntityManager em;

    /**
     * Implemented method from abstract facade.
     */
    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    /**
     * Constructor.
     */
    public CommentFacade() {
        super(Comment.class);
    }

    public List<Comment> findByContent(Content content) {
        Query q = em.createQuery("SELECT c FROM Comment c WHERE c.content = :content ORDER BY c.id DESC", Comment.class);
        q.setParameter("content", content);
        return q.getResultList();
    }

}
